package com.springboot.framework.service.impl;


import com.springboot.framework.config.WechatAccountConfig;
import com.springboot.framework.dao.entity.OrderDetail;
import com.springboot.framework.enums.Errors;
import com.springboot.framework.exception.BusinessException;
import com.springboot.framework.model.OrderDetailModel;
import com.springboot.framework.model.OrderModel;
import com.springboot.framework.model.SellerModel;
import com.springboot.framework.service.PushMessageService;
import com.springboot.framework.service.SellerService;
import com.springboot.framework.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * Created by 廖师兄
 * 2017-07-30 22:09
 */
@Service
@Slf4j
public class PushMessageServiceImpl implements PushMessageService {

    @Resource
    private WxMpService wxMpService;

    @Resource
    private WechatAccountConfig accountConfig;

    @Resource
    private SellerService sellerService;

    @Override
    public void newOrder(OrderModel orderModel) {
        List<SellerModel> sellerModels = sellerService.listByShopId(orderModel.getShopId());
        if (sellerModels == null) {
            throw new BusinessException(Errors.SHOP_NOT_OWNER);
        }
        StringBuilder products = new StringBuilder();
        for (OrderDetailModel orderDetail : orderModel.getOrderDetailModels()) {
            products.append(orderDetail.getProductName())
                    .append(" x")
                    .append(orderDetail.getQuantity())
                    .append(" \n");
        }

        WxMpTemplateMessage templateMessage = new WxMpTemplateMessage();
        templateMessage.setTemplateId(accountConfig.getTemplateId().get("orderStatus"));
        templateMessage.setUrl("http://tzs.yuanfudashi.com/#/pages/apply/sellerOrder");

        for (SellerModel sellerModel : sellerModels) {
            // 发送的openId
            templateMessage.setToUser(sellerModel.getWechatOpenid());

            List<WxMpTemplateData> data = Arrays.asList(
                    new WxMpTemplateData("first", "有新的订单"),
                    new WxMpTemplateData("keyword1", products.toString(), "#173177"),
                    new WxMpTemplateData("keyword2", orderModel.getPayableAmount().toString(), "#173177"),
                    new WxMpTemplateData("keyword3", orderModel.getUserName() + " / " + orderModel.getUserPhone(), "#173177"),
                    new WxMpTemplateData("keyword4", orderModel.getUserAddress(), "#173177"),
                    new WxMpTemplateData("keyword5", StringUtil.isEmpty(orderModel.getRemark()) ? "无" : orderModel.getRemark(), "#173177"),
                    new WxMpTemplateData("remark", "请尽快配送")
            );
            templateMessage.setData(data);
            try {
                wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            } catch (WxErrorException e) {
                log.error("【微信模版消息】发送失败, {}", e);
            }
        }
    }
}
